# -* - coding: UTF-8 -* -
#import sys
#reload(sys) 
#sys.setdefaultencoding('utf8')
import sys,os
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import threading,string,os
from rpc.thread import RPCThread
from time import sleep
from task import *
import logging

#从外部队列读取消息放入内部对列
class RpcFactory():  
	def __init__(self,serverFactory,clientFactory,service_center,config):
		self.serverFactory=serverFactory
		self.clientFactory=clientFactory
		self.service_center=service_center
		self.config=config
		self.threads=[]
		self.create()
	
		
	#生成队列
	def create(self):
		#RPC队列
		rpc_count=self.config.get_int('coop','rpc_count',1)
		self.rpc_server=self.serverFactory.create(self.config,self.config.get('queue_server','rpc_name','rpc'))
		for i in range(rpc_count):#10线程执行
			client=self.clientFactory.create(self.config)
			t = RPCThread(client,self.rpc_server,self.service_center)  
			self.threads.append(t)	
			
	#启动
	def start(self):
		for t in self.threads:
			t.start_thread()
	
	#停止
	def stop(self):
		logging.info("RPC Thread Stop!")			
		for t in self.threads:
			t.stop_thread()	
	#停止
	def terminate(self):
		logging.info("RPC Thread Stop!")			
		for t in self.threads:
			t.terminate()				

